package tools;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import javassist.expr.NewArray;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import util.AdBottomLog;

public class TimeTest {
	public final static Logger impLogger = LoggerFactory.getLogger("impinfo");
	static MemcachedClient memcachedClient;

	public static void main(String[] args) throws TimeoutException, InterruptedException, MemcachedException,
			IOException {
		String string = "null	1	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null	null";
		System.out.println(string.getBytes().length);
		long startTimeLong = System.currentTimeMillis();
		for (int i = 0; i < 200; i++) {
			AdBottomLog adBottomLog = new AdBottomLog();
			adBottomLog.setAdId(System.currentTimeMillis() + "");
			impLogger.info(adBottomLog.toString());
			System.out.println("writing" + i);
		}
		long s1 = System.currentTimeMillis() - startTimeLong;
		System.out.println("写入20000个数据花费" + (System.currentTimeMillis() - startTimeLong) + "ms");
		startTimeLong = System.currentTimeMillis();
		for (int i = 0; i < 200; i++) {
			AdBottomLog adBottomLog = new AdBottomLog();
			adBottomLog.setAdId(System.currentTimeMillis() + "");
			StringBuilder builder = memcachedClient.get("hello");
			builder.append("\n").append(adBottomLog);
			memcachedClient.replace("hello", 0, builder);
			System.out.println("cachewriting" + i);

		}
		System.out.println("cache写入20000个数据花费" + (System.currentTimeMillis() - startTimeLong) + "ms");
		String a = memcachedClient.get("hello").toString();

		System.out.println(a.length());
		impLogger.info(a);
		System.out.println("cache写入20000个数据花费" + (System.currentTimeMillis() - startTimeLong) + "ms");
		System.out.println("写入20000个数据花费" + s1 + "ms");
		// 111写入20000个数据花费121ms
		// 写入20000个数据花费17582ms
		memcachedClient.shutdown();
	}

	static {
		try {
			MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));
			memcachedClient = builder.build();
			memcachedClient.set("hello", 0, new StringBuilder(""));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}

	public static void main2(String[] args) throws Exception {
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(

		AddrUtil.getAddresses("localhost:11211"));

		MemcachedClient memcachedClient = builder.build();

		try {

			memcachedClient.set("hello", 0, "Hello,xmemcached");

			String value = memcachedClient.get("hello");

			System.out.println("hello=" + value);

			memcachedClient.delete("hello");

			value = memcachedClient.get("hello");

			System.out.println("hello=" + value);

		} catch (MemcachedException e) {

			System.err.println("MemcachedClient operation fail");

			e.printStackTrace();

		} catch (InterruptedException e) {

			// ignore

		}

		try {

			memcachedClient.shutdown();

		} catch (IOException e) {

			System.err.println("Shutdown MemcachedClient fail");

			e.printStackTrace();

		}

	}
}